package com.naicheng.ivote.views;

import java.util.HashMap;

import org.json.JSONObject;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.naicheng.ivote.R;
import com.naicheng.ivote.constant.ConstantMine;
import com.naicheng.ivote.doman.BaseResponseEntity;
import com.naicheng.ivote.doman.RuleBean;
import com.naicheng.ivote.engin.BaseActivity;
import com.naicheng.ivote.parser.BaseParser;
import com.naicheng.ivote.volley.Response.Listener;

public class MyIntegHandsel extends BaseActivity {

	RuleBean bean;

	private EditText et_integ;
	private TextView tv_integ;
	private EditText et_user;
	private TextView tv_send;
	private TextView main_title1;
	
	TextView tv_back;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myinteg_handsel);
		et_integ = (EditText) findViewById(R.id.et_integ);
		tv_integ = (TextView) findViewById(R.id.tv_integ);
		et_user = (EditText) findViewById(R.id.et_user);
		tv_send = (TextView) findViewById(R.id.tv_send);
		main_title1 = (TextView) findViewById(R.id.main_title1);
		
		tv_back = (TextView) this.findViewById(R.id.tv_back);
		bean = (RuleBean) getIntent().getSerializableExtra("bean");
		fillPage();

		tv_send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				send();

			}
		});
		
		tv_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void fillPage() {
		if (bean != null) {
			tv_integ.setText(String.valueOf(bean.integral));
		}
	}

	/**
	 * sendintegral userid True int 用户ID touserid True int 接收方用户ID integral
	 */
	private void send() {
		String username = et_user.getText().toString().trim();
		String integ = et_integ.getText().toString().trim();

		if (TextUtils.isEmpty(integ)) {
			Toast.makeText(getApplicationContext(), "请输入要赠送的积分数",
					Toast.LENGTH_LONG).show();
			return;
		}
		if (TextUtils.isEmpty(username)) {
			Toast.makeText(getApplicationContext(), "请输入要赠送的用户ID",
					Toast.LENGTH_LONG).show();
			return;
		}
		try {
			int a = Integer.parseInt(integ);
		} catch (NumberFormatException e) {
			Toast.makeText(getApplicationContext(), "请输入正确的积分数",
					Toast.LENGTH_LONG).show();
			e.printStackTrace();
			return;
		}

		HashMap<String, String> map = new HashMap<String, String>();
		map.put("userid", ConstantMine.userid);
		map.put("touserid", et_user.getText().toString().trim());
		map.put("integral", et_integ.getText().toString().trim());

		super.getDataFromServer("sendintegral", map,
				new Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject response) {
						System.out.println("response------>"
								+ response.toString());
						BaseResponseEntity<String> mResponse = new BaseParser<String>()
								.parserString(response);
						if (mResponse.getState()) {
							Toast.makeText(getApplicationContext(),
									mResponse.getMsg(), Toast.LENGTH_LONG)
									.show();
							finish();
						} else {
							Toast.makeText(getApplicationContext(),
									mResponse.getMsg(), Toast.LENGTH_LONG)
									.show();
						}

					}
				}, this);
	}

}
